/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : May 9, 2012  1:34:45 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : apps.business.process.commons.customer
 * File Name : CustomerProcessing.java
 * TODO
 */
package apps.business.process.commons.customer;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;

import jpa.entitys.synchro.staging.SdCustomerOutb1;

import org.apache.log4j.Logger;

import promis.utility.CommonService;

import sap.business.sd.outbound.VwSyncMktCustomerService;

import apps.sessionFactory;
import apps.business.process.SyncProcessorCommons;
import apps.promis.out.sales.VwSyncMktCustomerAction;

/**
 * @author chatree
 *
 */
public class CustomerProcessing extends SyncProcessorCommons{
	
	private static final Logger log = Logger.getLogger(CustomerProcessing.class);
	
	//private EntityManager  EmPromis = null;
	private EntityManager  EmSynchro = null; 
	
	/**
	 * @throws Exception 
	 * 
	 */
	public CustomerProcessing() throws Exception {
		//EmPromis = sessionFactory.CreateEmPromis();
		EmSynchro = sessionFactory.CreateEmSynchro();
	}
	
	public CustomerProcessing(EntityManager EmPromis, EntityManager EmSynchro) throws Exception {
		try{
			
			//this.EmPromis = EmPromis;			
			this.EmSynchro = EmSynchro;
			
		}catch(Exception e){
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
	}
	
	/**
	 * 
	 * @param customerCode
	 * @return
	 * @throws Exception
	 */
	public List<SdCustomerOutb1> FindCustomer(String customerCode) throws Exception {
		try{
			
			if(!CommonService.IsNull(customerCode))
				return new VwSyncMktCustomerAction().FindCustomer(customerCode);
			else
				return null;
			
		}catch(Exception e){
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	public List<SdCustomerOutb1> findAll() throws Exception {
		try{
			
			return new VwSyncMktCustomerAction().findAll();
			
		}catch(Exception e){
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	/**
	 * 
	 * @param customerCode
	 * @param status
	 * @throws Exception
	 */
	public void Processing(String customerCode,String status) throws Exception {
		try{
			
			//VwSyncMktCustomerAction objCustomer = new VwSyncMktCustomerAction(EmPromis, EmSynchro);			
			VwSyncMktCustomerAction objCustomer = new VwSyncMktCustomerAction();
			//String customerCode = "10000086";
			//String status = "A";
			//String syncIsstaging = "N";
			
			UPD_PROMISUseTime(true, false , null);
			System.out.println("Befor Create Customer to Staging CustomerCode =" + customerCode);
			List<SdCustomerOutb1> cus_lst = objCustomer.CreateToStaging(customerCode);
			System.out.println("Customer Size =" + CommonService.getSize(cus_lst));
			log.info("Create Customer [" + customerCode + "] || instalnce NO " + cus_lst.get(0).getInstanceNo() + " To staging ---> Size=" + CommonService.getSize(cus_lst));
			System.out.println("Create Customer [" + customerCode + "] || instalnce NO " + cus_lst.get(0).getInstanceNo() + " To staging ---> Size=" + CommonService.getSize(cus_lst));
			UPD_PROMISUseTime(false, true, null);
			
						
			if(!CommonService.IsNull(cus_lst)){
				//Sending To SAP ===>
				UPD_SENDUseTime(true, false, cus_lst.get(0).getInstanceNo());
				new VwSyncMktCustomerService().SendingToSAP(cus_lst);
				log.info("Sending Customer [" + customerCode + "] || instalnce NO " + cus_lst.get(0).getInstanceNo() + " To SAP ---> Size=" + CommonService.getSize(cus_lst));
				System.out.println("Sending Customer [" + customerCode + "] || instalnce NO " + cus_lst.get(0).getInstanceNo() + " To SAP ---> Size=" + CommonService.getSize(cus_lst));
				UPD_SENDUseTime(false, true, cus_lst.get(0).getInstanceNo());
			}else
				log.warn("Find not founs customer [" + customerCode + "] in PROMIS --->");
			
		}catch(Exception e){	
			e.printStackTrace();
			log.error(e.getMessage());
			System.err.println(e.getMessage());			
			throw e;
		}
	}
	
	private void Start() throws Exception {
		try{
			
			VwSyncMktCustomerService VwSyncMktCustomerServ = new VwSyncMktCustomerService();
			VwSyncMktCustomerAction objCustomer = new VwSyncMktCustomerAction();			
			String customerCode = "45184";
			//String status = "A";
			
			//Save To Staging
			//log.info("Start Create Customer To Staging =============================================>");
			//List<SdCustomerOutb1> cus_lst = objCustomer.CreateToStaging(customerCode);
			int i = 0;
			//List<String> lst = objCustomer.fincdCustomerAll();
			List<String> lst = new ArrayList<String>();
			//lst.add(customerCode);
			
			lst.add("10048070");
			lst.add("10048135");
			lst.add("10048164");
			lst.add("10048164");
			lst.add("10048203");
			lst.add("10048205");
			lst.add("10048243");
			lst.add("10048259");
			lst.add("10048259");
			lst.add("10048283");
			lst.add("10048290");
			lst.add("10048294");
			lst.add("10048297");
			lst.add("10048304");
			lst.add("10048321");
			lst.add("10048321");
			lst.add("10048324");
			lst.add("10048335");
			lst.add("10048343");
			lst.add("10048343");
			lst.add("10048355");
			lst.add("10048364");
			lst.add("10048366");
			lst.add("10048366");
			lst.add("10048376");
			lst.add("10048458");
			lst.add("10048459");
			lst.add("10048492");
			lst.add("10048492");
			lst.add("10048538");
			lst.add("10048541");
			lst.add("10048545");
			lst.add("10048560");
			lst.add("10048560");
			lst.add("10048624");
			lst.add("10048630");
			lst.add("10048632");
			lst.add("10048632");
			lst.add("10048659");
			lst.add("10048668");
			lst.add("10048668");
			lst.add("10048668");
			lst.add("10048671");
			lst.add("10048678");
			lst.add("10048705");
			lst.add("10048705");
			lst.add("10048714");
			lst.add("10048728");
			lst.add("10048774");
			lst.add("10048775");
			lst.add("10048804");
			lst.add("10048830");
			lst.add("10048850");
			lst.add("10048860");
			lst.add("10048866");
			lst.add("10048867");
			lst.add("10048879");
			lst.add("10048888");
			lst.add("10048918");
			
			if(lst != null){
				log.info(" All Customer = " + lst.size());
				for(String value : lst){
					i++;
					log.info(i + ".Sended Customer To SAP =============================================> Customer Code = " + value);
					
					try{
						
						//Create to Staging
						List<SdCustomerOutb1> cus_lst = new VwSyncMktCustomerAction().CreateToStaging(value); 
						//Send To SAP
						new VwSyncMktCustomerService().SendingToSAP(cus_lst);
						
					}catch(Exception e){
						log.error(e.getMessage());
						System.err.println(e.getMessage());
					}									
				}			
			}
		}catch(Exception e){			
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	public void ProcessingAll() throws Exception {
		try{
			
			List<String> lst = new VwSyncMktCustomerAction().fincdCustomerAll();
			if(lst != null){
				if(lst.size() <= 8)
				{
					for(String value : lst){
						try{
							//Create to Staging
							List<SdCustomerOutb1> cus_lst = new VwSyncMktCustomerAction().CreateToStaging(value);
							
							//Send To SAP
							new VwSyncMktCustomerService().SendingToSAP(cus_lst);
							
						}catch(Exception e){
							log.error(e.getMessage());
							System.err.println(e.getMessage());
						}
					}
	    		}
			}
		}catch(Exception e){
			log.error(e);
			throw e;
		}
	}
	
	public void ProcessingInStagingAll(String IsSync, String sync_created) throws Exception {
		try{
			
			List<Object[]> lst = new VwSyncMktCustomerAction().SearchCustomerAllInStagingIsSync(IsSync, sync_created);
			log.info("Customer list size=" + CommonService.getSize(lst));
			
				if(CommonService.getSize(lst) > 0)
				{
					int i = 0;
					for(Object[] value : lst){
						try{
							i++;
							String customer_code = value[0].toString();
							
							//Create to Staging
							List<SdCustomerOutb1> cus_lst = new VwSyncMktCustomerAction().CreateToStaging(customer_code);
							
							//Send To SAP
							new VwSyncMktCustomerService().SendingToSAP(cus_lst);
							log.info(i + ".Sending Customer to SAP size=" + CommonService.getSize(cus_lst));
							System.out.println(i + ".Sending Customer to SAP size=" + CommonService.getSize(cus_lst));
							
						}catch(Exception e){
							log.error(e.getMessage());
							System.err.println(e.getMessage());
						}
					}
				}
		}catch(Exception e){
			log.error(e);
			throw e;
		}
	}
	
	public void ProcessingNotSyncInStaging() throws Exception {
		log.info("ProcessingNotSyncInStaging start   --->");
		try{
			List<Object[]> lst = new VwSyncMktCustomerAction().SearchNotSyncInStaging();
			if(CommonService.getSize(lst) > 0)
			{
				int i = 0;
				for(Object[] value : lst){
					try{
						i++;
						String customerCode = value[0].toString();
						String instanceNo = value[1].toString();
						
						System.out.println("ProcessingNotSyncInStaging customerCode=" + customerCode + " || instanceNo=" +instanceNo);
						List<SdCustomerOutb1> cus_lst = new VwSyncMktCustomerAction().FindInstanceNo(customerCode, instanceNo);
						
						//Send To SAP
						new VwSyncMktCustomerService().SendingToSAP(cus_lst);
						log.info(i + ".Sending Customer to SAP size=" + CommonService.getSize(cus_lst));
						System.out.println(i + ".Sending Customer to SAP size=" + CommonService.getSize(cus_lst));
						
					}catch(Exception e){
						log.error(e.getMessage());
						System.err.println(e.getMessage());						
					}
				}
			}
		}catch(Exception e){
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		try{
			
			new CustomerProcessing().Start();
			
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
	}
}
